package android.tournament.client;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class GamesDbAdapter {
	
    public static final String KEY_TEAM1 = "team1";
    public static final String KEY_TEAM2 = "team2";
    public static final String KEY_SCORE1 = "score1";
    public static final String Key_SCORE2 = "score2";
    public static final String KEY_GAMEID = "_id";

    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "games";
    private static final int DATABASE_VERSION = 1;

    private static final String TAG = "GamesDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    /**
     * Database creation sql statement
     * _id game ID
     * team1, team2 names of the participating teams
     * score1, score2 scores of respective participating teams
     * 
     * No we are NOT using a relational database. I know java, not SQL
     */
    private static final String DATABASE_CREATE =
        "create table " + DATABASE_TABLE + " (_id integer primary key autoincrement, "
        + "team1 text not null, team2 text not null, score1 integer, score2 integer);";

    private final Context mCtx;
	
	private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }
	
    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public GamesDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }
    
    /**
     * Open the notes database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public GamesDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }
    
    public long UpdateGame(GameStatus status) {
    	ContentValues initial = new ContentValues();
    	
    	int gameId = status.hashCode();
    	
    	// initial.put(key, value);
    	
    	return mDb.update(DATABASE_TABLE, initial, KEY_GAMEID + "=" + gameId, null) -1;
    }

}
